package com.jade.linkong.controller;

import com.jade.common.annotation.Log;
import com.jade.common.constant.UserConstants;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.core.domain.Ztree;
import com.jade.common.core.page.TableDataInfo;
import com.jade.common.enums.BusinessType;
import com.jade.common.utils.StringUtils;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.*;
import com.jade.linkong.service.*;
import com.jade.system.domain.SysUser;
import com.jade.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 项目Controller
 * 
 * @author jade
 * @date 2020-07-13
 */
@Controller
@RequestMapping("/linkong/project")
public class LkProjectController extends BaseController
{
    private String prefix = "linkong/project";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ILkTemplateService lkTemplateService;

    @Autowired
    private ILkMatterService lkMatterService;

    @Autowired
    private ILkMatterProjectService lkMatterProjectService;

    @Autowired
    private ILkProjectTagService lkProjectTagService;




    @Autowired
    private ILkProjectService lkProjectService;

    @RequiresPermissions("linkong:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("linkong:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LkProject lkProject)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        if(StringUtils.isNotEmpty(sysUser.getRoleKey()) && StringUtils.equals("company", sysUser.getRoleKey())){
            lkProject.setLeaderPhone(sysUser.getPhonenumber());
        }
        startPage();
        List<LkProject> list = lkProjectService.selectLkProjectList(lkProject);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("linkong:project:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkProject lkProject)
    {
        List<LkProject> list = lkProjectService.selectLkProjectList(lkProject);
        ExcelUtil<LkProject> util = new ExcelUtil<LkProject>(LkProject.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        List<LkTemplate> lkTemplates = lkTemplateService.selectLkTemplateList(null);
        map.addAttribute("temps", lkTemplates);
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("linkong:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LkProject lkProject)
    {
        return toAjax(lkProjectService.insertLkProject(lkProject));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{proId}")
    public String edit(@PathVariable("proId") String proId, @RequestParam(required = false) String flag, ModelMap mmap)
    {
        LkProject lkProject = lkProjectService.selectLkProjectById(proId);
        mmap.put("lkProject", lkProject);
        if(StringUtils.isNotEmpty(flag) && StringUtils.equals(flag,"see")){
            return  "linkong/project/detail";
        }
        return prefix + "/edit";
    }

    /**
     * 修改事项
     */
    @GetMapping("/matter_edit/{proId}")
    public String matter_edit(@PathVariable("proId") String proId,String ids, ModelMap mmap)
    {
        mmap.put("ids", ids);
        mmap.put("proId", proId);
        SysUser sysUser = lkProjectService.selectUserByPhone(proId);
        int i = lkProjectService.matterCountByProject(Integer.valueOf(proId));
        if(i > 0){
            mmap.put("has_matter_flag", "true");
        }else{
            mmap.put("has_matter_flag", "false");
        }
        if(sysUser != null){
            mmap.put("userId", sysUser.getUserId());
            mmap.put("username", sysUser.getUserName());
            mmap.put("leader", sysUser.getLeader());
        }

        return prefix + "/matteredit";
    }
    
    /**
     * 审核事项
     */
    @GetMapping("/matter_audit/{proId}")
    public String matter_audit(@PathVariable("proId") String proId,String ids,String flasAuditOrView , ModelMap mmap)
    {
        mmap.put("ids", ids);
        mmap.put("proId", proId);
        mmap.put("flasAuditOrView", flasAuditOrView);
        return prefix + "/matterAudit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("linkong:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkProject lkProject)
    {
        return toAjax(lkProjectService.updateLkProject(lkProject));
    }

    /**
     * 选择项目标签树
     */
    @GetMapping(value = { "/selectProjectTagTree/{tabId}", "/selectProjectTagTree/" })
    public String selectProjectTagTree(@PathVariable(value = "tabId", required = false) Long tabId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(tabId))
        {
            mmap.put("lkProjectTag", lkProjectTagService.selectLkProjectTagById(tabId));
        }
        return prefix + "/tree";
    }
    /**
     * 删除项目
     */
    @RequiresPermissions("linkong:project:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lkProjectService.deleteLkProjectByIds(ids));
    }

    @PostMapping( "/proNameCheck")
    @ResponseBody
    public String proNameCheck(String proName){

        int i = lkProjectService.proNameCheck(proName);
        if(i > 0){
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }

   @PostMapping( "/selectMattersByIds")
   @ResponseBody
   public Map<String,List<LkMatter>> selectMattersByIds(String ids, int proId){

       Map<String,List<LkMatter>> map = lkMatterService.selectMattersByIds(ids,proId);
       return map;
   }

    @PostMapping( "/selectMattersByIds2")
    @ResponseBody
    public Map<String,Map<String, List<LkMatterProjectDto>>> selectMattersByIds( long proId){

        Map<String,Map<String, List<LkMatterProjectDto>>> map = lkMatterService.findListByProjectId2(proId);
        return map;
    }

    @GetMapping("/selectProjectBacklog")
    public String selectProjectBacklog(){
        return prefix + "/backlog";
    }

    @PostMapping( "/selectProjectByName")
    @ResponseBody
    public List<LkMatterProject> selectProjectByName(){
        LkMatterProject lkMatterProject = new LkMatterProject();
        lkMatterProject.setMainOrg(ShiroUtils.getUserId().toString());
        List<LkMatterProject> matterProjectList = lkMatterProjectService.selectLkMatterProjectList(lkMatterProject);
        for (LkMatterProject matterProject :matterProjectList) {
            LkProject project = lkProjectService.selectLkProjectById(matterProject.getProjectId().toString());
            matterProject.setProjectName(project.getProName());
            matterProject.setProMatterIds(project.getProMatterIds());
            matterProject.setProjectName(project.getProName());
        }
        return matterProjectList;
    }
    
    /**
     * 加载企业列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = userService.userTreeData();
        return ztrees;
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(LkProject lkProject)
    {
        SysUser user = new SysUser();
        if(StringUtils.isNotEmpty(lkProject.getLeaderPhone())){
            user.setPhonenumber(lkProject.getLeaderPhone());
            return userService.checkPhoneUnique(user);
        }

       return "";
    }

}
